package com.uenpay.im.ui.old.activity.red;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aswife.ui.RoundedImageView;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.weight.view.MyListView;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.util.app.base.BaseActivity;

import java.text.MessageFormat;

/**
 * @author SL
 * @created 2016-1-15 11:28:41
 * @copyRight http://www.qiyunxin.com
 * @function 红包详情(登录用户发送的私聊红包，并未被对方领取)
 */
public class RedDetailPersonalActivity extends BaseActivity {

    private TextView  send_red_content_tv;
    private ReceiveRedEntity receiveRedEntity = null;
    private MyListView listView;
    private RedDetailPersonalAdapter mAdapter;
    private TextView check_my_red_record_tv, red_overdue,is_save_tv;
    private RoundedImageView imageView;
    private TextView send_red_name_tv,send_red_money_tv;
    private LinearLayout money_layout,wait_layout;
    private TextView red_total_money_tv,spendtime_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);

        Intent balanece =new Intent();
        balanece.setAction("balance_change");
        sendBroadcast(balanece);
    }

    @Override
    public View bindView() {
        return null;


    }


    @Override
    public int bindLayout() {
        return R.layout.activity_red_detail_no_receive_layout;
    }

    @Override
    public void initView(View view) {
        receiveRedEntity = (ReceiveRedEntity) getIntent().getSerializableExtra(
                "mReceiveRedEntity");
        initView();
        initListener();
    }

    protected void initView() {
        check_my_red_record_tv = (TextView) findViewById(R.id.check_my_red_record_tv);
        red_overdue = (TextView) findViewById(R.id.red_overdue);
        send_red_content_tv = (TextView) findViewById(R.id.send_red_content_tv);
        listView = (MyListView) findViewById(R.id.listview);

        imageView = (RoundedImageView) findViewById(R.id.user_iv);
        send_red_name_tv = (TextView) findViewById(R.id.send_red_name_tv);
        send_red_money_tv = (TextView) findViewById(R.id.send_red_money_tv);
        money_layout = (LinearLayout) findViewById(R.id.money_layout);
        is_save_tv = (TextView) findViewById(R.id.is_save_tv);
        wait_layout = (LinearLayout) findViewById(R.id.wait_layout);
        red_total_money_tv = (TextView) findViewById(R.id.red_total_money_tv);
        spendtime_tv = (TextView) findViewById(R.id.spendtime_tv);

        if (receiveRedEntity != null) {
            imageView.SetUrl(APIConfiguration.getAvatarUrlNormal(
                    receiveRedEntity.custId, PacketType.PERSONAL));
            send_red_name_tv.setText(MessageFormat.format(getResources()
                            .getString(R.string.red_who_send),
                    receiveRedEntity.nickName));
            send_red_money_tv.setText(receiveRedEntity.amount);
            if (!TextUtils.isEmpty(receiveRedEntity.pickAmount)) {
                double pickAmount = Double.valueOf(receiveRedEntity.pickAmount);
                if (pickAmount > 0) {
                    send_red_money_tv.setText(receiveRedEntity.pickAmount);
                    money_layout.setVisibility(View.VISIBLE);
                    is_save_tv.setVisibility(View.VISIBLE);
                }
            }
            if (TextUtils.isEmpty(receiveRedEntity.blessing)) {
                send_red_content_tv.setText(R.string.red_default_content);
            } else {
                send_red_content_tv.setText(receiveRedEntity.blessing);
            }

            if (receiveRedEntity.custId.equals(IMApplication.getCustId())) {
//				red_total_money_tv.setText(MessageFormat.format(getResources()
//						.getString(R.string.red_total_count),
//						receiveRedEntity.number, receiveRedEntity.amount));
                wait_layout.setVisibility(View.VISIBLE);
            } else {
//				red_total_money_tv.setText(MessageFormat.format(getResources()
//						.getString(R.string.red_received_count),
//						receiveRedEntity.opennumber, receiveRedEntity.number));
                wait_layout.setVisibility(View.VISIBLE);
                check_my_red_record_tv.setVisibility(View.VISIBLE);
            }
            red_total_money_tv.setText(MessageFormat.format(getResources().getString(R.string.red_received_count),
                    receiveRedEntity.opennumber, receiveRedEntity.number));
            if (receiveRedEntity.leftPacketsNum == 0
                    && receiveRedEntity.spendtime > 0) {
                int h = 0;
                int d = 0;
                int s = 0;
                int temp = (int) (receiveRedEntity.spendtime % 3600);
                if (receiveRedEntity.spendtime > 3600) {
                    h = (int) (receiveRedEntity.spendtime / 3600);
                    if (temp != 0) {
                        if (temp > 60) {
                            d = temp / 60;
                            if (temp % 60 != 0) {
                                s = temp % 60;
                            }
                        } else {
                            s = temp;
                        }
                    }
                } else {
                    d = (int) (receiveRedEntity.spendtime / 60);
                    if (receiveRedEntity.spendtime % 60 != 0) {
                        s = (int) (receiveRedEntity.spendtime % 60);
                    }
                }
                if (d == 0) {
                    spendtime_tv.setText(MessageFormat.format(getResources()
                                    .getString(R.string.spendtime_second),
                            receiveRedEntity.spendtime));
                } else if (d > 0 && h == 0) {
                    spendtime_tv.setText(MessageFormat.format(getResources()
                            .getString(R.string.spendtime_minute), d, s));
                } else if (h > 0) {
                    spendtime_tv.setText(MessageFormat.format(getResources()
                            .getString(R.string.spendtime_hour), h, d, s));
                }
            } else {
                if (receiveRedEntity.custId.equals(IMApplication
                        .getCustId())) {
                    // TODO
                    spendtime_tv.setText(MessageFormat.format(getResources().getString(R.string.open_amount), receiveRedEntity.openamount, receiveRedEntity.amount));
                }
            }


            /**
             * 未打开，已过期
             */
            if (receiveRedEntity.isOpened == 0 && receiveRedEntity.expired != 0
                    && receiveRedEntity.arrayList.size() == 0
                    && receiveRedEntity.opennumber == 0) {
                red_overdue.setVisibility(View.VISIBLE);
            } else {
                mAdapter = new RedDetailPersonalAdapter(
                        RedDetailPersonalActivity.this,
                        receiveRedEntity.arrayList);
                listView.setAdapter(mAdapter);
                if (receiveRedEntity.custId.equals(IMApplication.getCustId())) {
                    check_my_red_record_tv.setVisibility(View.VISIBLE);
                    check_my_red_record_tv.setText(R.string.return_red_envelope);
                    check_my_red_record_tv.setTextColor(getResources().getColor(R.color.the_color_pale_gray));
                } else {
                    check_my_red_record_tv.setVisibility(View.VISIBLE);
                }
            }
        }

    }

    protected void initListener() {
        //返回键
        findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        check_my_red_record_tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(RedDetailPersonalActivity.this,
                        ReceviedOrSendRedActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
            }
        });
    }
}

